import { GetServerSideProps } from "next"
import { Post } from "../../types"
import PageLayout from "../../segments/PageLayout"

import * as postService from "../../server/app/PostService"

const PostPage: React.FC<{ path: string; post: Post }> = ({ path, post }) => {
  return (
    <PageLayout>
      <div className="post-layout">
        <div className="left">{path}</div>
        <div className="post">
          <h1>{post.title}</h1>
          {(() => {
            switch (post.type) {
              case "adoc":
                return <div className="adoc" dangerouslySetInnerHTML={{ __html: post.body }} />
              default:
                return <div>暂不支持的文档类型:{post.type}</div>
            }
          })()}
        </div>
        <div className="right">TODO:</div>
      </div>
      <div className="post-mobile-toc">{path}</div>
    </PageLayout>
  )
}

export default PostPage

export const getServerSideProps: GetServerSideProps = async (context) => {
  const postKey = context.params?.path as string
  const $post = await postService.findPostByKey(postKey)

  if (!$post) {
    return { notFound: true }
  } else {
    return {
      props: { path: postKey, post: $post }, // will be passed to the page component as props
    }
  }
}
